package org.sjs.dreamfly.biz.movie

import android.annotation.SuppressLint
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import org.sjs.dreamfly.R
import org.sjs.dreamfly.common.base.BaseFragment
import org.sjs.dreamfly.common.util.LogUtil
import org.sjs.dreamfly.common.util.Router
import org.sjs.dreamfly.data.Config

class MovieHomeFragment: BaseFragment() {

    private lateinit var btnAvatar: ImageView
    private lateinit var btnSearch: View
    private lateinit var btnReward: ImageView
    private lateinit var viewPager: ViewPager2
    private lateinit var tabLayout: TabLayout
    private val tabMap = Config.xiguaChannelList
    private val tabList = tabMap.keys.toList()

    private val fragmentList = mapOf<String, MovieListFragment>()

    override fun requireLayoutId(): Int {
        return R.layout.fragment_movie_home
    }

    override fun onBindViews() {
        val view = requireView()
        viewPager = view.findViewById(R.id.view_pager)
        tabLayout = view.findViewById(R.id.tab_layout)
        btnAvatar = view.findViewById(R.id.btn_avatar)
        btnSearch = view.findViewById(R.id.btn_search)
        btnReward = view.findViewById(R.id.btn_reward)
    }

    override fun onInitViews() {
        initTabLayout()
        initRecyclerView()
        btnSearch.setOnClickListener {
            MovieSearchDialog().show(requireActivity().supportFragmentManager, null)
        }
        btnAvatar.setOnClickListener {
            Router.launchPersonalActivity(context)
        }
    }

    override fun onLoadData() {
    }

    private fun initRecyclerView() {
        viewPager.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int { return tabList.size }
            override fun createFragment(position: Int): Fragment {
                val tab = tabList[position]
                return fragmentList[tab]?:XiGuaMovieListFragment.newInstance(tab)
            }
        }
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                tabLayout.selectTab(tabLayout.getTabAt(position))
                LogUtil.d("$position-${tabLayout.getTabAt(position)?.text}")
            }
        })
    }
    private fun initTabLayout() {
        LogUtil.d("initTabLayout tabLayout:${tabLayout}")
        tabList.forEach {
            val tab: TabLayout.Tab = tabLayout.newTab()
            tab.setCustomView(R.layout.custom_tab)
            tab.customView!!.findViewById<TextView>(R.id.tab_title).text = tabMap[it]
            tabLayout.addTab(tab)
        }
        tabLayout.tabGravity = TabLayout.GRAVITY_START
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            @SuppressLint("CutPasteId")
            override fun onTabSelected(tab: TabLayout.Tab) {
                LogUtil.d("onTabSelected pos = " + tab.position)
                updateCustomView(tab, true)
                viewPager.setCurrentItem(tab.position, true)
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                LogUtil.d("onTabUnselected pos = " + tab.position)
                updateCustomView(tab, false)
            }

            override fun onTabReselected(tab: TabLayout.Tab) {
                LogUtil.d("onTabReselected pos = " + tab.position)
                updateCustomView(tab, true)
            }

            private fun updateCustomView(
                tab: TabLayout.Tab,
                selected: Boolean /*int visible, int white*/
            ) {
                if (tab.customView == null) return
                val indicator = tab.customView!!.findViewById<ImageView>(R.id.tab_indicator)
                val textView = tab.customView!!.findViewById<TextView>(R.id.tab_title)
                if (indicator != null) {
                    indicator.visibility = if (selected) View.VISIBLE else View.INVISIBLE
                }
                textView?.setTextColor(
                    ContextCompat.getColor(
                        textView.context, if (selected) R.color.primary else R.color.black
                    )
                )
            }
        })
    }
}